﻿Imports System.IO

Public Class Test : Implements IEquatable(Of Test)

    'Private rutaFicheros As String = My.Application.Info.DirectoryPath & "\Ficheros"
    Public Property Preguntas As New List(Of Pregunta)
    Public Property Tema As String
    Public Sub New(ByVal tema As String)
        If Not Directory.Exists(My.Application.Info.DirectoryPath & "\Ficheros") Then
            Directory.CreateDirectory(My.Application.Info.DirectoryPath & "\Ficheros") 'Crea directorio Ficheros
        End If
        Me.Tema = tema
        Dim fich As StreamReader
        If File.Exists(My.Application.Info.DirectoryPath & "\Ficheros\" & tema & ".txt") Then
            'If File.Exists("Ficheros\" & tema & ".txt") Then
            Try
                fich = New StreamReader(My.Application.Info.DirectoryPath & "\Ficheros\" & tema & ".txt")
                Do Until fich.EndOfStream
                    Dim datos() As String
                    Dim Linea As String
                    Linea = fich.ReadLine
                    datos = Linea.Split("*")
                    Preguntas.Add(New Pregunta(datos(0), datos(1)))
                Loop
            Catch ex As Exception
                Preguntas.Clear()
            Finally
                fich.Close()

            End Try

        Else
            Grabar()


        End If



       
    End Sub
    Public Function Añadir(ByVal preg As Pregunta) As String
        If IsNothing(preg) Then
            Return "Pregunta no válida"
        End If
        If preg.Enunciado = "" Then
            Return "Falta el enunciado de la pregunta"
        End If
        If IsNothing(preg.Respuesta) Then
            Return "Falta la respuesta de la pregunta"
        End If
        If Preguntas.Equals(preg) Then
            Return "La pregunta ya existe"
        End If
        Preguntas.Add(preg)
        Grabar()
        Return ""
    End Function
    Public Function Modificar(ByVal pregvieja As Pregunta, ByVal pregNueva As Pregunta) As String
        If Not Preguntas.Contains(pregvieja) Then
            Return "No existe esa pregunta"
        End If
        For i As Integer = 0 To Preguntas.Count - 1
            If Preguntas(i).Equals(pregvieja) Then
                Preguntas(i) = pregNueva
                Grabar()
                Return ""
            End If
        Next
        Return "Error"
    End Function
    Public Function Borrar(ByVal preg As Pregunta) As String
        If Not Preguntas.Contains(preg) Then
            Return "No existe esa pregunta"
        End If
        For i As Integer = 0 To Preguntas.Count - 1
            If Preguntas(i).Equals(preg) Then
                Preguntas.RemoveAt(i)
                Grabar()
                Return ""
            End If
        Next
        Return "Error"
    End Function
    Public Function Grabar() As String
        Dim fich As StreamWriter
        Try

            If Not Directory.Exists(My.Application.Info.DirectoryPath & "\Ficheros") Then
                Directory.CreateDirectory(My.Application.Info.DirectoryPath & "\Ficheros") 'Crea directorio Ficheros
            End If
            File.Delete(My.Application.Info.DirectoryPath & "\Ficheros\" & Tema & ".txt")
            fich = New StreamWriter(My.Application.Info.DirectoryPath & "\Ficheros\" & Tema & ".txt")
            For i As Integer = 0 To Preguntas.Count - 1
                fich.WriteLine(Preguntas(i).Enunciado & "*" & Preguntas(i).Respuesta)

            Next

        Catch ex As Exception
            fich.Close()
            Return ex.Message
        End Try
        fich.Close()
        Return ""

    End Function
    Public Sub BorrarTodas()
        Preguntas.Clear()
        Grabar()
    End Sub
    Public Overloads Function Equals(ByVal other As Test) As Boolean Implements System.IEquatable(Of Test).Equals
        Return other IsNot Nothing AndAlso Me.Tema.ToLower = other.Tema.ToLower

    End Function

End Class
